package com.example.chenduoyun.safe.activity;
/**
 * @author cdy
 */

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.chenduoyun.safe.R;
import com.example.chenduoyun.safe.utils.ConstantValue;
import com.example.chenduoyun.safe.utils.Md5Util;
import com.example.chenduoyun.safe.utils.SpUtils;
import com.example.chenduoyun.safe.utils.ToastUtil;

public class HomeActivity extends AppCompatActivity {

    private GridView gv_home;
    private int[] mDrawableIds;
    private String[] mTitleStrs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initUI();
        initData();
    }

    private void initData() {
        mTitleStrs = new String[]{
                "手机防盗", "通讯卫士", "软件管理",
                "进程管理", "流量统计", "手机杀毒",
                "缓存清理", "高级工具", "设置中心"
        };
        mDrawableIds = new int[]{
                R.drawable.home_safe, R.drawable.home_callmsgsafe, R.drawable.home_apps,
                R.drawable.home_taskmanager, R.drawable.home_netmanager, R.drawable.home_trojan,
                R.drawable.home_sysoptimize, R.drawable.home_tools, R.drawable.home_settings
        };

        gv_home.setAdapter(new MyAdapter());

        gv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        showDialog();
//                        Intent(getApplicationContext(),);
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                    case 8:
                        Intent intent = new Intent(getApplicationContext(), SettingActivity.class);
                        startActivity(intent);
                        break;


                }
            }
        });
    }

    /**
     * 判断本地是否有密码
     * 1.初始设置密码对话框
     * 2.确认密码对话框
     */
    private void showDialog() {
        String psd = SpUtils.getString(this, ConstantValue.MOBILE_SAFE_PSD, "");
        if (TextUtils.isEmpty(psd)) {
            showSetPsdDialog();
        } else {
            showConfirmPsdDialog();
        }
    }

    private void showConfirmPsdDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        final View view = View.inflate(this, R.layout.dialog_confirm_psd, null);
        dialog.setView(view,0,0,0,0);
        dialog.show();
        view.findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et_confirm_psd = view.findViewById(R.id.et_confirm_psd);
                String confirmPsd= et_confirm_psd.getText().toString().toString();
                String psd = SpUtils.getString(getApplicationContext(),ConstantValue.MOBILE_SAFE_PSD,"");
                if(psd.equals(Md5Util.encoder(confirmPsd))){
                    dialog.dismiss();
                    Intent intent = new Intent(getApplicationContext(), MobileSafeActivity.class);
                    startActivity(intent);
                }else{
                    ToastUtil.show(getApplicationContext(),"输入密码不一致");
                }
            }
        });
        view.findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    private void showSetPsdDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        final View view = View.inflate(this, R.layout.dialog_set_psd, null);
        dialog.setView(view,0,0,0,0);
        dialog.show();
        Button bt_submit = view.findViewById(R.id.bt_submit);
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et_confirm_psd = view.findViewById(R.id.et_confirm_psd);
                EditText et_set_psd = view.findViewById(R.id.et_set_psd);
                String psd = et_set_psd.getText().toString();
                String confirmpsd = et_confirm_psd.getText().toString();
                if (!TextUtils.isEmpty(psd) && psd.equals(confirmpsd)) {
                    Intent intent = new Intent(getApplicationContext(), MobileSafeActivity.class);
                    startActivity(intent);
                    dialog.dismiss();
                    SpUtils.putString(getApplicationContext(), ConstantValue.MOBILE_SAFE_PSD
                            , Md5Util.encoder(psd));
                } else {
                    ToastUtil.show(getApplicationContext(), "密码输入不一致");
                }

            }
        });
        view.findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    private void initUI() {
        gv_home = findViewById(R.id.gv_home);

    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mTitleStrs.length;
        }

        @Override
        public Object getItem(int position) {
            return mTitleStrs[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getApplicationContext(), R.layout.gridview_item, null);
            TextView tv_title = view.findViewById(R.id.tv_title);
            ImageView iv_icon = view.findViewById(R.id.iv_icon);
            tv_title.setText(mTitleStrs[position]);
            iv_icon.setBackgroundResource(mDrawableIds[position]);
            return view;
        }
    }

}
